﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using System.Windows.Controls;

using SBPweb.Presentation.CompleteMesh.Utils;

namespace SBPweb.Presentation.CompleteMesh.Controls
{
    /// <summary>
    /// Interaction logic for ViewPort.xaml
    /// </summary>
    public partial class ViewPort : UserControl
    {

        #region Constructor logic

        public ViewPort()
        {
            InitializeComponent();
        }

        #endregion

        #region Public property accessors

        public Grid MainGrid
        {
            get
            {
                return gridMain;
            }
        }

        public Viewport3D MainViewPort
        {
            get
            {
                return vpMain;
            }
        }

        public PerspectiveCamera MainCamera
        {
            get
            {
                return camMain;
            }
            set
            {
                camMain = value;
            }
        }

        public Model3DGroup MainContentModelGroup
        {
            get
            {
                return m3dgMain;
            }
            set
            {
                foreach (Model3D m3d in value.Children)
                {
                    m3dgMain.Children.Add(m3d);
                }
            }
        }

        public ModelVisual3D MainChildrenModel
        {
            get
            {
                return mv3dMain;
            }
            set
            {
                foreach (Visual3D v3d in value.Children)
                {
                    mv3dMain.Children.Add(v3d);
                }
            }
        }

        public AmbientLight AmbientLight
        {
            get
            {
                return alightMain;
            }
            set
            {
                alightMain = value;
            }
        }

        #endregion

        #region Event handlers

        #endregion

        #region Public helper methods

        public void InitializeCameraTrackBall()
        {
            Trackball trackBall = new Trackball();
            trackBall.EventSource = rectMain;
            camMain.Transform = trackBall.Transform;
        }

        public void AnimateCameraZoom(double delta, double duration)
        {
            Point3DAnimation anim = new Point3DAnimation();
            Vector3D vect = new Vector3D(   MainCamera.Position.X + MainCamera.LookDirection.X,
                                            MainCamera.Position.Y + MainCamera.LookDirection.Y,
                                            MainCamera.Position.Z + MainCamera.LookDirection.Z);
            vect = Vector3D.Multiply(vect, delta * 10.0f);

            anim.From = MainCamera.Position;
            anim.To = Point3D.Add(MainCamera.Position, vect);

            anim.Duration = new Duration(TimeSpan.FromMilliseconds(duration));
            anim.AccelerationRatio = 0.2f;
            anim.DecelerationRatio = 0.7f;

            MainCamera.BeginAnimation(PerspectiveCamera.PositionProperty, anim);
        }

        public void AnimateCameraPan(Vector vector, double duration)
        {
            Point3DAnimation anim = new Point3DAnimation();      

            RotateTransform3D cameraSpin = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(vector.Y, vector.X, 0), 10)); 
    
            cameraSpin.CenterX = MainCamera.Position.X + MainCamera.LookDirection.X;
            cameraSpin.CenterY = MainCamera.Position.Y + MainCamera.LookDirection.Y;
            cameraSpin.CenterZ = MainCamera.Position.Z + MainCamera.LookDirection.Z;

            anim.From = MainCamera.Position;
            anim.To = cameraSpin.Transform(MainCamera.Position);

            anim.Duration = new Duration(TimeSpan.FromMilliseconds(duration));
            anim.AccelerationRatio = 0.2f;
            anim.DecelerationRatio = 0.7f;

            MainCamera.BeginAnimation(PerspectiveCamera.PositionProperty, anim);
        }

        #endregion

    }
}
